// 导入必要的库和工具
import { defineStore } from 'pinia'; // Pinia状态管理库
import { getLeftData } from '@/api/earlyWarning'; // API请求方法
import { onMounted, ref } from 'vue'; // Vue响应式API
import { gasUsersInfo } from '@/constants/components/gasUsers'; // 燃气用户信息常量
import { mapBackendData, type moduleInfo } from '@/utils/mapBackendData';
import componentsTitle from '@/constants/components/componentsTitle';

// 管线类型映射配置(中文名称与后端字段的映射关系)
const GASUSERS_TYPE_MAPPING = {
  [componentsTitle.RESIDENT_USERS]: { countKey: 'residentUsers' },
  [componentsTitle.INDUSTRIAL_USERS]: { countKey: 'industrialUsers' },
  [componentsTitle.BUSINESS_USERS]: { countKey: 'commerceUsers' },
  [componentsTitle.PUBLIC_AND_CHARITY_USERS]: { countKey: 'publicWelfareUsers' }
} as const; // as const确保类型推断为字面量类型

export const useGasUsersStore = defineStore('gasUsers', () => {
  const gasUsers = ref<moduleInfo[]>(gasUsersInfo);

  onMounted(async () => {
    const res = await getLeftData({ type: 'userStatistics' });
    gasUsers.value = mapBackendData(gasUsersInfo, res.data.userStatistics, GASUSERS_TYPE_MAPPING);
  });
  return {
    gasUsers
  };
});
